<template>
  <div class="home" style="margin-left: 5px;width: auto">
    <!--    搜索区域-->
    <div style="margin: 10px">
      <el-input v-model="search" size="large" placeholder="请输入要搜索的菜品名称" style="width: 40%" clearable />
      <el-button type="primary" size="large" style="margin-left: 5px" @click="pageList"><el-icon><search /></el-icon>搜索</el-button>
    </div>
    <!--    功能区域-->
    <div style="margin: 10px">
<!--      <el-button type="primary" @click="add">新增菜品</el-button>-->
      <el-popconfirm title="确认删除吗?" @confirm="deleteByIds">
        <template #reference>
          <el-button type="danger">批量删除</el-button>
        </template>
      </el-popconfirm>
    </div>
    <!--    数据区域-->
    <el-table :data="tableData" @selection-change="handleSelectionChange" border stripe style="width: calc(100vw - 205px)">
      <el-table-column type="selection" width="55" />
<!--      <el-table-column :resizable="false" prop="id" label="id" />-->
      <el-table-column :resizable="false" label="菜品图片">
        <template #default="scope">
          <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.img"
              :preview-src-list="[scope.row.img]"
              :initial-index="4"
              fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column :resizable="false" prop="name" label="菜品名称" />
      <el-table-column :resizable="false" prop="price" label="菜品价格" />
      <el-table-column :resizable="false" label="菜品数量">
        <template #default="scope">
          <el-input-number v-model="scope.row.num" size="small" :min="1" :max="scope.row.sum" @change="handleChange(scope.row)" />
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="165px">
        <template #default="scope">
          <!--          删除按钮-->
          <el-popconfirm title="确认删除吗?" @confirm="handleDelet(scope.row.id)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
<!--    &lt;!&ndash;    分页插件&ndash;&gt;-->
<!--    <div style="margin: 10px">-->
<!--      <el-pagination-->
<!--          :currentPage="currentPage"-->
<!--          :page-size="pageSize"-->
<!--          :page-sizes="[5, 9]"-->
<!--          layout="total, sizes, prev, pager, next, jumper"-->
<!--          :total="total"-->
<!--          @size-change="handleSizeChange"-->
<!--          @current-change="handleCurrentChange"-->
<!--      />-->
<!--    </div>-->
    <div style="margin: 10px 0;text-align: right;">
      当前总价:¥  <span style="color: orangered"><b>{{totalPrice}}</b></span>
    </div>
    <div style="margin: 10px 0;text-align: right">
      <el-button style="background-color: orangered;color: white" size="large" @click="settleAccount"><el-icon><coin /></el-icon>结算</el-button>
    </div>

    <!--    地址模态窗口-->
    <el-dialog
        v-model="dialogVisible"
        title="选择地址"
        width="30%"
    >
      <!--      窗口内的表单-->
      <el-form :model="form" label-width="120px">
        <el-form-item label="会员价格">
          <div style="text-align: center"><span style="color:orangered;"><b> ¥ {{(this.totalPrice * this.discount).toFixed(2)}}</b></span></div>
        </el-form-item>
        <el-form-item label="我的地址">
          <el-input style="width: 80%" v-model="form.address" />
        </el-form-item>
        <el-form-item label="我的电话">
          <el-input style="width: 80%" v-model="form.phone" />
        </el-form-item>
        <el-form-item label="订单备注">
          <el-input type="textarea" style="width: 80%" v-model="form.remark" />
        </el-form-item>
      </el-form>
      <!--新增的模态窗口的确认按钮-->
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确认</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src
import request from "@/utils/request";
import {Search,Coin} from "@element-plus/icons";
export default {
  name: 'HomeView',
  components: {
    //注册
    Search,Coin
  },
  data() {
    return {
      form:{},
      dialogVisible:false,
      search:'',
      total:0,
      currentPage:1,
      pageSize:3,
      tableData:[],
      selectType:[],
      carts: [],
      totalPrice: 0,
      user: sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {},
      discount: 1,

    }
  },
  created() {
    this.pageList()
    this.getMyVip()
  },
  methods:{
    //获得用户vip等级
    getMyVip(){
      request.get("order/vip/getMyVip",{params:{id:this.user.id}}).then(res =>{
        this.discount=res.data.discount
      })
    },
    // 选择完地址后点击确认结算
    save(){
      let data = {
        totalPrice:this.totalPrice * this.discount,
        address: this.form.address,
        phone: this.form.phone,
        remark: this.form.remark,
        foods:[]
      }
      console.log(data)
      this.carts.forEach(v => {
        data.foods.push({id: v.foodId,sale: v.num});
      })
      request.post("order/order/save",data).then(res => {
        this.$router.push("/front/order");
      })
    },
    //点击结算按钮
    settleAccount() {
      if(!this.carts.length || !this.carts){
        this.$message.warning("请选择要结算的菜品!");
      }else{
        this.dialogVisible = true;
        this.user = sessionStorage.getItem("user")?JSON.parse(sessionStorage.getItem("user")):{}
        this.form.address=this.user.address
        this.form.phone=this.user.phone
      }
    },
    //改变购物车表的数量
    handleChange(row){
      request.post("order/cart/handleChange",{num:row.num,foodId:row.foodId})
      this.totalPrice = 0;
      this.carts.forEach(item => {
        if(item.name == row.name){
          // console.log(item.img)
          this.totalPrice += item.price * row.num;
          item.num = row.num
        }else{
          this.totalPrice += item.price * item.num;
        }
      })
    },
    deleteByIds() {
      // console.log(this.ids)
      if(!this.ids.length){
        this.$message.error("请选择要删除的数据!");
        return
      }
      //批量删除
      request.post("order/cart/deleteByIds/",this.ids).then(res => {
        this.msgTip("删除",res);
        this.pageList();
      })
    },
    handleSelectionChange(val) {
      //选中的数据
      this.carts=val;
      // console.log(this.carts)
      this.totalPrice = 0;
      if(val && val.length){
        val.forEach(item => {
          this.totalPrice += item.price * item.num;
        })
      }
    },
    sucLoad(response) {
      //图片上传成功做的操作,1.访问上传的url返回response.data是图片地址2.赋值给表单的img元素
      this.form.img=response.data
    },
    pageList(){
      request.get("order/cart/pageList", {
        params: {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          name: this.search
        }
      }).then(res =>{
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    handleDelet(id){
      //删除
      request.delete("order/cart/delete/" + id).then(res => {
        this.msgTip("删除",res);
        this.pageList();
      })
    },
    handleSizeChange(pageSize){
      this.pageSize=pageSize;
      this.pageList()
    },
    handleCurrentChange(currentPage){
      this.currentPage=currentPage;
      this.pageList()
    },
    msgTip(msg,res){
      if(res.code == 200){
        this.$message({
          type:"success",
          message: msg+"成功"
        })
      }else{
        this.$message({
          type:"error",
          message:res.msg
        })
      }
    }
  }
}
</script>

<style>
/*.el-popper {*/
/*  z-index: 100000 !important;*/
/*}*/
/*/deep/.el-select-dropdown {*/
/*  position: absolute !important;*/
/*  top: 50px !important;*/
/*  left: 0 !important;*/
/*}*/
</style>